package task3;

/**
 * 通话套餐类
 */
public class PhonePackage extends Package implements PhoneService{
    // 通话时长
    private double time;
    // 短信条数
    private int message_count;

    // 无参构造
    public PhonePackage() {
    }

    // 有参构造
    public PhonePackage(double price, double time, int message_count) {
        super(price);
        this.time = time;
        this.message_count = message_count;
    }

    public double getTime() {
        return time;
    }

    public void setTime(double time) {
        this.time = time;
    }

    public int getMessage_count() {
        return message_count;
    }

    public void setMessage_count(int message_count) {
        this.message_count = message_count;
    }

    // 显示所有套餐信息
    @Override
    public void show() {
        System.out.println("当前通话套餐信息为：");
        System.out.println("剩余通话时长：" + this.getTime() + "分钟  剩余短信：" + this.getMessage_count() + "条  每月资费：" + super.getPrice() + "元");
    }

    // 实现通话服务接口
    @Override
    public void service(double time, PhoneCard phoneCard) {
        // 如果通话套餐内通话时长小于等于已经消费的通话时长+当前通话时长，则套餐用完
        if (phoneCard.getPhonePackage().getTime() <= time){
            System.out.println("套餐内通话时长已用完！");
        }
        // 如果通话套餐内通话时长大于已经消费的通话时长+当前通话时长，则套餐还有剩余，可继续使用套餐内通话
        else {
            // 套餐内通话时长减少
            phoneCard.getPhonePackage().setTime(phoneCard.getPhonePackage().getTime() - time);
            // 当前电话卡通话时间增加
            phoneCard.setTime(phoneCard.getTime() + time);
        }
    }

    // 添加通话套餐，并消费套餐金额
    @Override
    public void addPackage(PhoneCard phoneCard,UserConsume userConsume){
        // 若当前电话卡无通话套餐
        if (phoneCard.getPhonePackage() == null){
            if (phoneCard.getBalance() >= this.getPrice()){
                // 为当前电话卡用户绑定通话套餐
                phoneCard.setPhonePackage(this);
                // 每月固定消费一次通话套餐金额
                userConsume.setFine(phoneCard.getPhonePackage().getPrice());
                phoneCard.setUserConsume(userConsume);
                phoneCard.setBalance(phoneCard.getBalance() - phoneCard.getPhonePackage().getPrice());
            }else {
                System.out.println("当前电话卡余额不足，不能办理该通话套餐！");
            }
        }else
        {
            System.out.println("当前电话卡已有通话套餐！");
        }
    }

    // 用户消费电话套餐
    @Override
    public void consume(PhoneCard phoneCard,UserConsume userConsume,double time){
        if (phoneCard.getPhonePackage() != null){
            userConsume.setTime(time);
            phoneCard.setUserConsume(userConsume);
            phoneCard.getPhonePackage().service(time,phoneCard);
        }
    }

}
